import { computed } from "vue";
import request from "@/config/axios/service";
import { formatTime } from "@/utils/index";
import { ElMessage } from "element-plus";

/**
 *
 * @param {*} componentValueList 组件变量列表(响应式)
 * @param {*} defaultFuntionList 默认的全局函数列表(响应式)
 * @param {*} pageList 视图列表(响应式)
 * @returns vars具有响应式的变量对象
 */
export const useWindowData = (
  componentValueList,
  defaultFuntionList,
  pageList
) => {
  const vars = computed(() => {
    let obj = {};
    componentValueList.value.map((item) => {
      obj[item.name] = item.value;
    });
    return obj;
  });
  defaultFuntionList.value.forEach((item) => {
    switch (item.name) {
      case "setValue":
        item.value = (name, newVal) => {
          componentValueList.value.filter(
            (item) => item.name === name
          )[0].value = newVal;
        };
        break;
      case "getValue":
        item.value = (name) =>
          componentValueList.value.filter((item) => item.name === name)[0]
            .value;
        break;
      case "openView":
        item.value = (id) => {
          pageList.value.filter((item) => item.id === id)[0].isShow = true;
        };
        break;
      case "closeView":
        item.value = (id) => {
          pageList.value.filter((item) => item.id === id)[0].isShow = false;
        };
        break;
      //------复杂函数在 json.parse 转化是有问题 ，所有在在这里赋值
      case "$request":
        item.value = request;
        break;
      case "$ElMessage":
        item.value = ElMessage;
        break;
      case "$formatTime":
        item.value = formatTime;
        break;
    }
    window[item.name] = item.value;
  });
  return {
    vars,
  };
};
